package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.common.BaseActivity;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.responsibility.DataResponsibility;

import java.io.File;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mac on 2018/4/1.
 */

public abstract class BaseAuthoActivity extends BaseActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_baseautho);
        CommonTitleBar titlebar = (CommonTitleBar) findViewById(R.id.titlebar);
        initTitleBarView(titlebar, getTitleName());
        ViewGroup root = (ViewGroup) findViewById(R.id.root);
        getLayoutInflater().inflate(getcontentView(), root, true);

    }

    protected Observable<PullAuthoBean> getAuthoData(String url) {
        return RetrofitManager.getInstance().create(AppAPI.class).getAuthoData(url, SPUtils.getInstance().getString("uid"))
                .compose(this.<PullAuthoBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 标题
     *
     * @return
     */
    protected abstract String getTitleName();

    /**
     * 布局文件
     *
     * @return
     */
    public abstract int getcontentView();

    /**
     * 封装上传图片
     *
     * @param url
     * @param paramMap
     * @param fileMap
     */
    protected Observable<CommonResonseBean> submitDataToServer(String url, HashMap<String, String> paramMap, HashMap<String, File> fileMap) {
        return DataResponsibility.getInstance().submitDataToServer(url, paramMap, fileMap, this);
        /*MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (paramMap != null) {

            Set<Map.Entry<String, String>> entries = paramMap.entrySet();
            Iterator<Map.Entry<String, String>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                builder.addFormDataPart(next.getKey(), next.getValue());
            }
        }
        if (fileMap != null) {
            Iterator<Map.Entry<String, File>> iterator1 = fileMap.entrySet().iterator();
            while (iterator1.hasNext()) {
                Map.Entry<String, File> next = iterator1.next();
                builder.addFormDataPart(next.getKey(), next.getValue().getName(), RequestBody.create(MediaType.parse("image*//*"), next.getValue()));
            }
        }

        MultipartBody build = builder.build();
        return RetrofitManager.create().authoData(url, build).
                compose(this.<CommonResonseBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread());*/

    }

    protected void showMsg(String msg) {
        ToastUtils.showShort(msg);
    }

    protected void showMsg() {
        showMsg("上传完成");
    }

    protected void loadImage(ImageView target, String img_url) {
        if (target == null) {
            return;
        }
        if (TextUtils.isEmpty(img_url)) {
            return;
        }
        Glide.with(this).load(Constant.IMAGE_PATH + img_url).into(target);
    }
}
